(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.siteCalendar.createVerifyController', createVerifyController);
	createVerifyController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'siteCalendarService' ];
	function createVerifyController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, siteCalendarService) {
		var ctrl = this, siteCalendarInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.siteCalendar = {};
		ctrl.init = function() {
			siteCalendarService.initOptions($scope, ctrl);
			var params = {
					ccy : siteCalendarInfo.ccy
			};
			authService.createVerify($scope.$stateParams.params).then(function(response) {
				ctrl.siteCalendar = response.data.model ;
				ctrl.siteCalendar.taskId = response.data.task.taskId;
				$scope.calendarYear = ctrl.siteCalendar.calendarDay.year;
			});
		};

		ctrl.createVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.siteCalendar.primaryKey]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.createVerifyApprove(ctrl.siteCalendar).then(approveSuccessful, saveFailed);
					}
				}
			});
		};

		ctrl.createVerifyReject = function() {			
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.siteCalendar.comments = comments;
						authService.createVerifyReject(ctrl.siteCalendar).then(rejectSuccessful, submitFailed);
					}
				}
			});			
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedSubCommpanyList = selectedRows;
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.sitecalendar.view',
					'state' : 'cda.siteCalendar.view',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.sitecalendar.update',
					'state' : 'cda.siteCalendar.update',
					'params' : {}
				}]
			});
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);